<?php

require_once 'Mail.php';

class MyMail {

    private $backend;
    private $params;
    private $recipients;
    private $headers;
    private $body;

    public function __construct($backend, $params = null) {
        $this->backend = $backend;
        if($params && is_array($params)) {
            $this->params = $params;
        }
        else {
            $this->params = array();
        }
    }

    private function configure_backend() {
        static $mailer = null;

        if(!$mailer) {
            $mailer = Mail::factory($this->backend, $this->params);

            if(PEAR::isError($mailer)) {
                return null;
            }
        }

        return $mailer;
    }

    public function send($recipients, $headers, $body) {
        if($mailer = $this->configure_backend()) {
            if(!isset($headers['Content-Type'])) {
                $headers['Content-Type'] = 'text/plain; charset=UTF-8';
            }
            $sent = $mailer->send($recipients, $headers, $body);
            if(PEAR::isError($sent)) {
                return false;
            }
            return true;
        }
        return false;
    }

}
